package com.sqpoll.android;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;

/**
 * The main activity.
 * 
 * @author Younggue Bae
 */
public class MainActivity extends SherlockFragmentActivity {

	public static final String TAG = "MainActivity";

	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_main);
		
		mViewPager = (ViewPager) findViewById(R.id.pager);
		//mViewPager = new ViewPager(this);
		//mViewPager.setId(R.id.pager);
		//setContentView(mViewPager);

		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		actionBar.setDisplayShowTitleEnabled(true);

		/* add tabs into action bar */
		mTabsAdapter = new TabsAdapter(this, mViewPager);

		//RelativeLayout view = (RelativeLayout) getLayoutInflater().inflate(R.layout.tab_label1, null);
		//Tab tab = actionBar.newTab().setCustomView(view);
		Tab tab = actionBar.newTab().setText(R.string.tab_home);
		mTabsAdapter.addTab(tab, HomeFragment.class, null);

		//view = (RelativeLayout) getLayoutInflater().inflate(R.layout.tab_label2, null);
		//tab = actionBar.newTab().setCustomView(view);
		tab = actionBar.newTab().setText(R.string.tab_group_poll);
		mTabsAdapter.addTab(tab, GroupListFragment.class, null);

		//view = (RelativeLayout) getLayoutInflater().inflate(R.layout.tab_label3, null);
		//tab = actionBar.newTab().setCustomView(view);
		tab = actionBar.newTab().setText(R.string.tab_hot_open_poll);
		mTabsAdapter.addTab(tab, OpenPollListFragment.class, null);
		
		//view = (RelativeLayout) getLayoutInflater().inflate(R.layout.tab_label4, null);
		//tab = actionBar.newTab().setCustomView(view);
		tab = actionBar.newTab().setText(R.string.tab_new_open_poll);
		mTabsAdapter.addTab(tab, OpenPollListFragment.class, null);
	}
	
	@Override
	public void onBackPressed() {
		
		final Fragment fragment = (Fragment) mTabsAdapter.getCurrentFragment();
		if (fragment instanceof GroupListFragment) {
			GroupListFragment fragmentGroup = (GroupListFragment) fragment;
			for (int i = 0; i < fragmentGroup.mListView.getChildCount(); i++) {
				View itemView = fragmentGroup.mListView.getChildAt(i);
				if (itemView != null) {
					View btnEdit = itemView.findViewById(R.id.button_edit);
					if (btnEdit.getVisibility() == View.VISIBLE) {
						btnEdit.setVisibility(View.INVISIBLE);
	
						View buttonExit = itemView.findViewById(R.id.button_exit);
						buttonExit.setVisibility(View.INVISIBLE);
						
						View badge = itemView.findViewById(R.id.new_poll_count);
						badge.setVisibility(View.VISIBLE);
					}
					else {
						super.onBackPressed();
						break;
					}
				}
			}
		}
		else {
			super.onBackPressed();
		}
	}

	/**
	 * This is a helper class that implements the management of tabs and all details of connecting a ViewPager with
	 * associated TabHost. It relies on a trick. Normally a tab host has a simple API for supplying a View or Intent that
	 * each tab will show. This is not sufficient for switching between pages. So instead we make the content part of the
	 * tab host 0dp high (it is not shown) and the TabsAdapter supplies its own dummy view to show as the tab content. It
	 * listens to changes in tabs, and takes care of switch to the correct paged in the ViewPager whenever the selected
	 * tab changes.
	 */
	public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener,
			ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final ActionBar mActionBar;
		private final ViewPager mViewPager;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
		@SuppressLint("UseSparseArrays")
		private Map<Integer, Fragment> mPageReferenceMap = new HashMap<Integer, Fragment>();
		
		static final class TabInfo {
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(Class<?> _class, Bundle _args) {
				clss = _class;
				args = _args;
			}
		}

		public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mActionBar = activity.getSupportActionBar();
			mViewPager = pager;
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
		}

		public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
			TabInfo info = new TabInfo(clss, args);
			tab.setTag(info);
			tab.setTabListener(this);
			mTabs.add(info);
			mActionBar.addTab(tab);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			Fragment fragment = Fragment.instantiate(mContext, info.clss.getName(), info.args);
			mPageReferenceMap.put(Integer.valueOf(position), fragment);
			
			return fragment;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			super.destroyItem(container, position, object);
			
			mPageReferenceMap.remove(Integer.valueOf(position));
		}

		@Override
		public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			mActionBar.setSelectedNavigationItem(position);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}

		@Override
		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			Object tag = tab.getTag();

			for (int i = 0; i < mTabs.size(); i++) {
				if (mTabs.get(i) == tag) {
					mViewPager.setCurrentItem(i);
					Log.d(TAG, "onTabSelected == " + tag);
				}
			}
		}

		@Override
		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		}

		@Override
		public void onTabReselected(Tab tab, FragmentTransaction ft) {
		}
		
		public Fragment getCurrentFragment() {
			int position = mViewPager.getCurrentItem();
			return mPageReferenceMap.get(position);
		}
	}

}